package com.tianzeculture.stagebao.activity.home;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.ArtistActivity;
import com.tianzeculture.stagebao.activity.business.BusinessActivity;
import com.tianzeculture.stagebao.activity.business.BusinessDetailActivity;
import com.tianzeculture.stagebao.activity.home.adapter.SearchFireAdapter;
import com.tianzeculture.stagebao.activity.home.adapter.SearchHistoryAdapter;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.net.HomeRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.SpUtils;
import com.tianzeculture.stagebao.util.Util;
import com.tianzeculture.stagebao.view.MyGridView;
import com.tianzeculture.stagebao.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/6/1 0001.
 */

public class SearchActivity extends BaseActivity {

    @BindView(R.id.edtSearch)
    EditText edtSearch;
    @BindView(R.id.tvCleanHistory)
    TextView tvCleanHistory;
    @BindView(R.id.tvNoHistory)
    TextView tvNoHistory;
    @BindView(R.id.lvHistory)
    MyListView lvHistory;

    @BindView(R.id.gvFire)
    MyGridView gvFire;

    @Request
    HomeRestUsage homeRestUsage = null;

    SearchFireAdapter fireAdapter;
    List<String> historyStrings = new ArrayList<>();
    SearchHistoryAdapter historyAdapter;
    private int searchId = 5;

    @Override
    public void initUI() {
        super.initUI();
        lvHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                homeRestUsage.searchName(searchId, historyStrings.get(i));
            }
        });
        initHistory();
//        initFire();
        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String searchString = edtSearch.getText().toString().trim();
                    String oldHistory = (String) SpUtils.get(SearchActivity.this, SpUtils.SEARCH_HISTORY, "");
                    Boolean isAdd = true;
                    for (int i = 0; i < historyStrings.size(); i++) {
                        if (searchString.equals(historyStrings.get(i))) {
                            isAdd = false;
                            break;
                        }
                    }
                    if (isAdd) {
                        if (!oldHistory.equals("")) {
                            historyStrings.add(searchString);
                            SpUtils.put(SearchActivity.this, SpUtils.SEARCH_HISTORY, searchString
                                    + "&stagebao#" + oldHistory);
                        } else {
                            SpUtils.put(SearchActivity.this, SpUtils.SEARCH_HISTORY,
                                    searchString);
                        }
                    }
                    if (historyAdapter != null)
                        historyAdapter.notifyDataSetChanged();
                    handled = true;
                    homeRestUsage.searchName(searchId, searchString);
                }
                return handled;
            }
        });


    }

    /**
     * 初始搜索历史
     */
    private void initHistory() {
        String s = (String) SpUtils.get(this, SpUtils.SEARCH_HISTORY, "");
        if (s.length() > 0) {
            tvNoHistory.setVisibility(View.GONE);
            lvHistory.setVisibility(View.VISIBLE);
            String[] sp = s.split("&stagebao#");
            historyStrings.clear();
            for (int i = 0; i < sp.length; i++) {
                historyStrings.add(sp[i]);
            }
            historyAdapter = new SearchHistoryAdapter(this, historyStrings);
            lvHistory.setAdapter(historyAdapter);
        } else {
            tvNoHistory.setVisibility(View.VISIBLE);
            lvHistory.setVisibility(View.GONE);
        }
    }

    /**
     * 初始热门搜索
     */
    private void initFire() {
        List<String> strings = new ArrayList<>();
        strings.add("男模");
        strings.add("女模");
        strings.add("小清新");
        strings.add("乐队");
        strings.add("男模");
        strings.add("女模");
        strings.add("小清新");
        strings.add("乐队");
        strings.add("男模");
        strings.add("女模");
        strings.add("小清新");
        strings.add("乐队");
        strings.add("男模");
        strings.add("女模");
        strings.add("小清新");
        strings.add("乐队");

        fireAdapter = new SearchFireAdapter(this, strings);
        gvFire.setAdapter(fireAdapter);
    }


    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == searchId) {
            ResultBean resultBean = (ResultBean) msg.getObj();
            List<User> myResult = resultBean.getMyResult();
            if (myResult.size() == 0) {
                ToastUtil.showShort(this, "未搜索到相关类容");
                return;
            }
            if ((myResult.get(0).getUserType() == 0))
                startActivity(new Intent(this, ArtistActivity.class).putExtra("otherId", myResult.get(0).getId()));
            else
                startActivity(new Intent(this, BusinessDetailActivity.class).putExtra("userId", myResult.get(0).getId()));
            finish();
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_search;
    }
}
